﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MM.Common.DBBase;
using System.Data;

namespace MM.Common.Base
{
    public static class GeneralDictionary
    {
        // Methods
        public static bool DeleteDict(string id)
        {
            DataParamCol parms = new DataParamCol(new DataParam[] { new InputDataParam("did", id) });
            return (SystemInfo.DbData.IsOpen && (SystemInfo.DbData.ExecuteSql("DELETE MXHLY_SYS_DICTIONARY WHERE DT_ID = :did", parms) == 1));
        }

        public static bool DeleteTableDict(string id)
        {
            DataParamCol parms = new DataParamCol(new DataParam[] { new InputDataParam("did", id) });
            return (SystemInfo.DbData.IsOpen && (SystemInfo.DbData.ExecuteSql("DELETE MXHLY_SYS_DICTIONARY_TABLE WHERE DT_TB_TABLE_CODE = :did", parms) == 1));
        }

        public static string GetDictCode(string type, string id)
        {
            DataParamCol parms = new DataParamCol(new DataParam[] { new InputDataParam("idvalue", id), new InputDataParam("type", type) });
            string commandtext = "SELECT DT_CODE FROM MXHLY_SYS_DICTIONARY WHERE DT_ID = :idvalue AND DT_TYPE = :type";
            try
            {
                string data = "";
                if (SystemInfo.DbData.ExecuteScalar<string>(commandtext, parms, ref data))
                {
                    return data;
                }
                return null;
            }
            catch
            {
                return null;
            }
        }

        public static string GetDictDefaultValue(string type_name)
        {
            DataParamCol parms = new DataParamCol(new DataParam[] { new InputDataParam("did", type_name) });
            if (SystemInfo.DbData.IsOpen)
            {
                string data = "";
                SystemInfo.DbData.ExecuteScalar<string>("SELECT DT_CODE FROM MXHLY_SYS_DICTIONARY WHERE DT_TYPE = :did AND DT_DEFAULT_IS = 'Y'", parms, ref data);
                return data;
            }
            return "";
        }

        public static int GetDictID(string type, string code)
        {
            DataParamCol parms = new DataParamCol(new DataParam[] { new InputDataParam("idvalue", code), new InputDataParam("type", type) });
            string commandtext = "SELECT DT_ID FROM MXHLY_SYS_DICTIONARY WHERE DT_CODE = :idvalue AND DT_TYPE = :type";
            try
            {
                int data = -1;
                SystemInfo.DbData.ExecuteScalar<int>(commandtext, parms, ref data);
                return data;
            }
            catch
            {
                return -1;
            }
        }

        public static DataTable GetDictList(string param_type, string param_where, bool param_use)
        {
            DataTable dt = new DataTable();
            DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("type", param_type) });
            if (SystemInfo.DbData.IsOpen)
            {
                StringBuilder builder = new StringBuilder();
                builder.Append("SELECT DT_ID, DT_TYPE, DT_NAME, DT_CODE, DT_USE FROM MXHLY_SYS_DICTIONARY WHERE DT_TYPE = :type");
                if (param_use)
                {
                    builder.Append(" AND DT_USE = 'Y' ");
                }
                else
                {
                    builder.Append(" AND DT_USE = 'N' ");
                }
                if (param_where.Length > 0)
                {
                    builder.Append(" and " + param_where);
                }
                SystemInfo.DbData.Retrieve(builder.ToString(), parm, ref dt);
            }
            return dt;
        }

        public static string GetDictNameByCode(string type, string code)
        {
            DataParamCol parms = new DataParamCol(new DataParam[] { new InputDataParam("idvalue", code), new InputDataParam("type", type) });
            string commandtext = "SELECT DT_NAME FROM MXHLY_SYS_DICTIONARY WHERE DT_CODE = :idvalue AND DT_TYPE = :type";
            try
            {
                string data = "";
                if (SystemInfo.DbData.ExecuteScalar<string>(commandtext, parms, ref data))
                {
                    return data;
                }
                return null;
            }
            catch
            {
                return null;
            }
        }

        public static object GetDictValue(string id)
        {
            return SystemInfo.DbData.GetValue("MXHLY_SYS_DICTIONARY", "DT_ID", id, "DT_NAME");
        }

        public static bool IsDictExist(string id)
        {
            return SystemInfo.DbData.IsExist("MXHLY_SYS_DICTIONARY", "DT_ID", id);
        }

        public static bool IsDictTableExist(string tabid)
        {
            return SystemInfo.DbData.IsExist("MXHLY_SYS_DICTIONARY_TABLE", "DT_TB_TABLE_CODE", tabid);
        }
    }
}
